* =====================================
* ------------- Two Step --------------
* =====================================


	use $dta/EstimationSample.dta, clear
	

	* Pool categories
	gen Work1Fam1Fam1Work1Any = 1*(Work1Fam1Any == 1 | Fam1Work1Any == 1)
	gen Work1Fam1Fam1Work1Same = 1*(Work1Fam1Same == 1 | Fam1Work1Same == 1)
	gen Imm1Fam1Fam1Imm1Any = 1*(Imm1Fam1Any == 1 | Fam1Imm1Any == 1)
	gen Imm1Fam1Fam1Imm1Same = 1*(Imm1Fam1Same == 1 | Fam1Imm1Same == 1)
	

	
* Estimate models
* ===============
	
	egen idbsu = group(Voter_id Voter_bsu)
	gen period = 1*(year == 2019)
	xtset idbsu period
	estimates clear
	
	
	* Work1Fam1
	preserve
	gen Any1a = Fam1Any
	gen Any1b = Work1Any
	gen Same1a = Fam1Same
	gen Same1b = Work1Same
	gen Any2 = Work1Fam1Any
	gen Same2 = Work1Fam1Same	
	qui sum Voter_turnout
	local meandepvar = string(r(mean)*100, "%15.2fc")
	eststo m_1: xtreg Voter_turnout Any* Same* i.period, ///
		i(idbsu) fe cluster(Voter_bsu)
	quietly estadd local meandep `meandepvar', replace
	restore

	
	* Fam1Work1
	preserve
	gen Any1a = Fam1Any
	gen Any1b = Work1Any
	gen Same1a = Fam1Same
	gen Same1b = Work1Same
	gen Any2 = Fam1Work1Any
	gen Same2 = Fam1Work1Same	
	qui sum Voter_turnout
	local meandepvar = string(r(mean)*100, "%15.2fc")
	eststo m_2: xtreg Voter_turnout Any* Same* i.period, ///
		i(idbsu) fe cluster(Voter_bsu)
	quietly estadd local meandep `meandepvar', replace
	restore
	
	
	* Pooled	
	preserve
	gen Any1a = Fam1Any
	gen Any1b = Work1Any
	gen Same1a = Fam1Same
	gen Same1b = Work1Same
	gen Any2 = Work1Fam1Fam1Work1Any
	gen Same2 = Work1Fam1Fam1Work1Same
	qui sum Voter_turnout
	local meandepvar = string(r(mean)*100, "%15.2fc")
	eststo m_3: xtreg Voter_turnout Any* Same* i.period, ///
		i(idbsu) fe cluster(Voter_bsu)
	quietly estadd local meandep `meandepvar', replace
	restore

	
	* Imm1Fam1
	preserve
	gen Any1a = Fam1Any
	gen Any1c = Imm1Any
	gen Same1a = Fam1Same
	gen Same1c = Imm1Same
	gen Any2 = Imm1Fam1Any
	gen Same2 = Imm1Fam1Same	
	qui sum Voter_turnout
	local meandepvar = string(r(mean)*100, "%15.2fc")
	eststo m_4: xtreg Voter_turnout Any* Same* i.period, ///
		i(idbsu) fe cluster(Voter_bsu)
	quietly estadd local meandep `meandepvar', replace
	restore

	
	* Fam1Imm1
	preserve
	gen Any1a = Fam1Any
	gen Any1c = Imm1Any
	gen Same1a = Fam1Same
	gen Same1c = Imm1Same
	gen Any2 = Fam1Imm1Any
	gen Same2 = Fam1Imm1Same	
	qui sum Voter_turnout
	local meandepvar = string(r(mean)*100, "%15.2fc")
	eststo m_5: xtreg Voter_turnout Any* Same* i.period, ///
		i(idbsu) fe cluster(Voter_bsu)
	quietly estadd local meandep `meandepvar', replace
	restore
	
	
	* Pooled	
	preserve
	gen Any1a = Fam1Any
	gen Any1c = Imm1Any
	gen Same1a = Fam1Same
	gen Same1c = Imm1Same
	gen Any2 = Imm1Fam1Fam1Imm1Any
	gen Same2 = Imm1Fam1Fam1Imm1Same
	qui sum Voter_turnout
	local meandepvar = string(r(mean)*100, "%15.2fc")
	eststo m_6: xtreg Voter_turnout Any* Same* i.period, ///
		i(idbsu) fe cluster(Voter_bsu)
	quietly estadd local meandep `meandepvar', replace
	restore


* Export table
* ============

	esttab  m_* ///
		using $tbls\TwoStep.tex, replace  style(tex) ///
		prehead("\begin{tabular}{l*{7}{c}} \hline\hline \\ ") ///
		posthead("\hline \\") ///
		fragment ///
		mtitles( ///
			"\shortstack{Candidate \\ $\rightarrow$ co-wkr.$\rightarrow$ \\ family}" ///
			"\shortstack{Candidate \\ $\rightarrow$ fam.$\rightarrow$ \\ co-worker}" ///
			"Pooled" ///
			"\shortstack{Candidate \\ $\rightarrow$ imm.$\rightarrow$ \\ family}" ///
			"\shortstack{Candidate \\ $\rightarrow$ fam.$\rightarrow$ \\ immigrant}" ///
			"Pooled") ///
		mgroups( "Co-workers and families" "Immigrants and families", ///
		pattern(1 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) ///
		end(\cmidrule(lr){2-4} \cmidrule(lr){5-7})) ///
		label se b(%9.3f) se(%9.3f) nostar ///
		refcat(Any2 "No candidate in network") ///
		order(Any2 Same2) ///
		coeflabels( ///
			Any2 "\shortstack{\\ \\ \\ Any District}" ///
			Same2 "Same District" ) ///	
		drop(_cons *.period Any1* Same1*) noomitted nobaselevels ///
		nonotes stats(N N_clust meandep, labels( ///
			"Observations" "Clusters" "Mean turnout (\%)") ///
			fmt(%15.0fc %15.0fc %15.0fc %15.0fc)) ///
		substitute(\_ _) ///
		prefoot("\hline") ///
		postfoot("\hline\hline \\ \end{tabular}")
		